import { ReactElement, useRef, useEffect } from 'react'
import { Modal, Form, Input, FormInstance, message } from 'antd'
import { DataSchema } from '../../ajax'
import UnitNumberInput from '@components/unitNumberInput'
import { WorkPerformanceSchema } from '@src/components/schema/performanceSchema'
import { awaitWrap } from '@js/tool'
import { addRequest, updateRequest } from './ajax'

const { Item } = Form
const { TextArea } = Input

async function submitHandle (d: { [k: string]: any }, work: WorkPerformanceSchema | null, workType: string, hide: () => void, refreshData: () => void): Promise<void> {
  if (work === null) {
    return undefined
  }
  const id: string = d.id
  let fn = addRequest
  if (typeof id !== 'string') {
    d.workid = work.id
    d.type = workType
  } else {
    fn = updateRequest
  }
  const h = message.loading('提交中')
  const [e] = await awaitWrap(fn(d))
  h()
  if (e === null) {
    message.success(typeof id === 'string' ? '更新成功' : '添加成功').then(null, null)
    hide()
    refreshData()
  }
}

interface Props {
  visible: boolean
  work: WorkPerformanceSchema | null
  workType: string
  data: DataSchema | null
  hide: () => void
  refreshData: () => void
}

function Main ({ visible, workType, data, work, hide, refreshData }: Props): ReactElement {
  const formRef = useRef<FormInstance>(null)
  let weightPlaceholder = '请输入'
  let placeholder = '原则上不高于1.5分'
  let min: number = 0
  let max: number = 1
  if (workType === '常规工作') {
    placeholder = '请输入分数，如1.25'
    weightPlaceholder = '总计不低于0.5'
    min = 0.5
  } else if (workType === '临时工作') {
    placeholder = '请输入分数，如1.25'
    weightPlaceholder = '总计不高于0.2'
    max = 0.2
  }
  useEffect(() => {
    const c = formRef.current
    if (data !== null && c !== null) {
      c.setFieldsValue(data)
    }
  }, [data, formRef])
  useEffect(() => {
    const c = formRef.current
    if (!visible && c !== null) {
      c.resetFields()
    }
  }, [visible, formRef])
  return (
    <Modal title={data === null ? `添加${workType}` : `修改${workType}`} visible={visible} maskClosable={false} onCancel={hide} onOk={() => formRef.current?.submit()}>
      <Form ref={formRef} labelCol={{ flex: '108px' }} onFinish={async d => await submitHandle(d, work, workType, hide, refreshData)}>
        <Item noStyle name='id'><span /></Item>
        <Item label='职责描述' name='content' rules={[{ required: true, message: '职责描述不能为空' }]}>
          <TextArea placeholder='请输入' autoSize={{ minRows: 3 }}></TextArea>
        </Item>
        <Item label='权重' name='weights' rules={[{ required: true, message: '权重不能为空' }]}>
          <UnitNumberInput min={min} max={max} precision={2} unit='' placeholder={weightPlaceholder} />
        </Item>
        <Item label='自我评价' name='self_evaluation_score' rules={[{ required: true, message: '自我评价不能为空' }]}>
          <UnitNumberInput unit='分' placeholder={placeholder} min={0} precision={2} />
        </Item>
        <Item label='直接上级评价' name='leadership_score' rules={[{ required: true, message: '直接上级评价不能为空' }]}>
          <UnitNumberInput unit='分' placeholder={placeholder} min={0} precision={2} />
        </Item>
      </Form>
    </Modal>
  )
}

export default Main
